package nio.application;

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.util.Date;
import java.util.Scanner;

/**
 * @Desc:
 * @author: cww
 * @DateTime: 2020-02-06 10:24
 */

public class NIOClient1 {
    public static void main(String[] args) throws Exception {
        System.out.println("客户端已经启动....");
        SocketChannel sChannel = SocketChannel.open(new InetSocketAddress("127.0.0.1", 7777));
        sChannel.configureBlocking(false);
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            String str = scanner.next();
            byteBuffer.put((new Date().toString() + "\n" + str).getBytes());
            byteBuffer.flip();
            sChannel.write(byteBuffer);
            byteBuffer.clear();
        }
        sChannel.close();
    }
}
